/*
 *  $Id: ElementalTrap.cs 1063 2010-05-20 17:40:43Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Neutral.Spells {
    public class ElementalTrap: SelfEnchantmentCard {
        public ElementalTrap(): base("001_n_s_elemental_trap") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Neutral;
            Value = 100;
            AddCondition(HandCardsCondition.OwnRelease(1));
        }

        protected override EnchantmentAffect CreateEnchantment() {
            return new ElementalTrapAffect();
        }

        private class ElementalTrapAffect: EnchantmentAffect<ElementalTrap> {
            private static readonly Languages.CardPhrase<ElementalTrap> NotActive = new Languages.CardPhrase<ElementalTrap>("affect.not_active.desc");
            private static readonly Languages.CardPhrase<ElementalTrap> Action = new Languages.CardPhrase<ElementalTrap>("action");

            private bool _active;

            public ElementalTrapAffect(): base(true) {
                DefaultDescription = true;
                _active = true;
                Add(new Affects.AbilityAffect(new ElementalTrapAbility(this)));
            }

            public override Languages.Phrase Description {
                get { return Active ? base.Description : NotActive.Phrase; }
            }

            public bool Active {
                get { return _active; }
                set {
                    _active = value;
                    UpdateDescription();
                }
            }

            public override void SetAffect() {
                base.SetAffect();
                EnemySummary.OnUseCard += DoCard;
                EnemySummary.OnUseAbility += DoAbility;
                GlobalSummary.OnStartOffencePhase += DoOffence;
            }

            public override void ResetAffect() {
                base.ResetAffect();
                EnemySummary.OnUseCard -= DoCard;
                EnemySummary.OnUseAbility -= DoAbility;
                GlobalSummary.OnStartOffencePhase -= DoOffence;
            }

            private void DoCard(Card card, TargetArgs args) {
                if (!Active) return;
                if (card.GetSumManaFor(args.Player) > 0) {
                    Game.Current.Log.Add(Action.Phrase);
                    Owner.Owner.IncrementMana(card.Element, 1);
                }
            }

            private void DoAbility(AbilityCard ability, TargetArgs args) {
                DoCard(ability, args);
            }

            private void DoOffence() {
                Active = false;
            }
        }

        private class ElementalTrapAbility: AbilityCard {
            private ElementalTrapAffect _affect;

            public ElementalTrapAbility(ElementalTrapAffect affect): base("001_n_a_elemental_trap") {
                Level = CompleteLevel.ReadyForTest;
                SetPictureSource<ElementalTrap>();
                Element = ElementType.Neutral;
                LogMessage = GetLogPhrase();
                AddCondition(HandCardsCondition.OwnRelease(1));
                AddCondition(new DelegateCondition(GetConditionPhrase(), NeedActivate));
                TapCreatureAfterUse = false;

                _affect = affect;
            }

            private bool NeedActivate(AbstractObject caster, Card card) {
                return _affect != null && !_affect.Active;
            }

            public override void DoAction(TargetArgs args) {
                if (_affect != null) {
                    _affect.Active = true;
                }
            }
        }
    }
}
